//Programa principal: Gerencia todas as etapas da decodificação.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "bitstream.h"
#include "sideinf.h"
#include "functions.h"
#include "scale_factor.h" 

int main(int argc, char *argv[])
{
	int i=0,tags_len,conta=0;
	FILE *fp;
	fp=fopen(argv[1],"r"); //Abre o arquivo para o stream

	tags_len=dados_musica(fp);  //Identificar tamanho total das tags

	tag_info(fp,tags_len);  //Ler as tags

	fseek(fp,tags_len+10,SEEK_SET);   //Posiciona o ponteiro no início do header

	while(!feof(fp))
	{
		header *hdr=inicia_header();
		header_prt1(hdr,fp);
		if (hdr->sync==2047  && hdr->bit_rate!=15  && hdr->sample!=3)
		{
			header_prt2(hdr);
			unread *num=malloc(sizeof(unread)); 
			num->quant=0;
			num->unbits=0;		
			printf("%d\n",hdr->frame_len);	
			getchar();				
	        sideinfo *sd=malloc(sizeof(sideinfo));   //
			scale_granulo *scgr=malloc(sizeof(scale_granulo));  //
			
//****************************************************************************************************//			
			
			int check[16];			
			if (crc) {
					for (int i=0;i<16;i++){	
						check[i]=get_bit(fp,num,8);		 		
						printf("%x ",check[i]);	
				}						
					printf("\n");			
			}
					
//***************************************************************************************************//
			
			inform(fp, num, channel,sd );
 			sd_gran(fp, num, channel, &sd->gran1 ); 	
			sd_gran(fp, num, channel, &sd->gran2 );
			pos_block(fp, num, channel,sd );
			fseek(fp,sd.main_data,SEEK_CUR);
			sdinfo_decode (fp, &sd -> gran1, &sd -> gran1 -> can1, unread *bits);
		
//**************************************************************************************************//
			/*
			printf("%d\n",sd->main_data);
			printf("%d\n",sd->private_bits);
			printf("%d\n",sd->scfsi[0]);
			printf("%d\n",sd->scfsi[1]);
			printf("%d\n",sd->gran1.part2_3_length[0]);
			printf("%d\n",sd->gran1.part2_3_length[1]);
			printf("%d\n",sd->gran1.big_values[0]);
			printf("%d\n",sd->gran1.big_values[1]);
			printf("%d\n",sd->gran1.global_gain[0]);
			printf("%d\n",sd->gran1.global_gain[1]);
			printf("%d\n",sd->gran1.scalefac_compress[0]);
			printf("%d\n",sd->gran1.scalefac_compress[1]);				
			printf("%d\n",sd->gran1.window_switching[0]);
			printf("%d\n",sd->gran1.window_switching[1]);
			printf("%d\n",sd->gran1.table_select[0]);
			printf("%d\n",sd->gran1.table_select[1]);			
			printf("%d\n",sd->gran1.table_select[2]);
			printf("%d\n",sd->gran1.table_select[3]);
			printf("%d\n",sd->gran1.table_select[4]);
			printf("%d\n",sd->gran1.table_select[5]);			
			printf("%d\n",sd->gran1.region0_count[0]);			
			printf("%d\n",sd->gran1.region0_count[1]);
			printf("%d\n",sd->gran1.region1_count[0]);
			printf("%d\n",sd->gran1.region1_count[1]);
			printf("%d\n",sd->gran2.part2_3_length[0]);
			printf("%d\n",sd->gran2.part2_3_length[1]);
			printf("%d\n",sd->gran2.big_values[0]);
			printf("%d\n",sd->gran2.big_values[1]);
			printf("%d\n",sd->gran2.global_gain[0]);
			printf("%d\n",sd->gran2.global_gain[1]);
			printf("%d\n",sd->gran2.scalefac_compress[0]);
			printf("%d\n",sd->gran2.scalefac_compress[1]);				
			printf("%d\n",sd->gran2.window_switching[0]);
			printf("%d\n",sd->gran2.window_switching[1]);
			printf("%d\n",sd->gran2.table_select[0]);
			printf("%d\n",sd->gran2.table_select[1]);			
			printf("%d\n",sd->gran2.table_select[2]);
			printf("%d\n",sd->gran2.table_select[3]);
			printf("%d\n",sd->gran2.table_select[4]);
			printf("%d\n",sd->gran2.table_select[5]);			
			printf("%d\n",sd->gran2.region0_count[0]);			
			printf("%d\n",sd->gran2.region0_count[1]);
			printf("%d\n",sd->gran2.region1_count[0]);
			printf("%d\n",sd->gran2.region1_count[1]);
			printf("%d\n",sd->preflag);
			printf("%d\n",sd->scalefac_scale);
			printf("%d\n",sd->count1table_select);
*/
			fseek(fp,hdr->frame_len-4,SEEK_CUR);
			conta=conta+hdr->frame_len;
		}
		else
		{
			conta++;
			fseek(fp,tags_len+10+conta,SEEK_SET);
		}
	}
	fclose(fp);
	return 0;
}
